package com.sample.systemui.viewmodel;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import androidx.lifecycle.ViewModel;

import com.sample.servicefragment.ViewMutableLiveData;
import com.sample.systemui.SystemUIApp;

public class StatusBarViewModel extends ViewModel {

    private static StatusBarViewModel sInstance = null;

    private static StatusBarLiveData<Integer, String> mViewLiveData = null;

    /**
     * sample005: ViewMutableLiveData 使用例子
     * 1.在ViewModel层定义 ViewMutableLiveData 变量--蓝牙状态。
     */
    private static ViewMutableLiveData<Integer> mBluetoothState = null;

    public StatusBarViewModel() {
        sInstance = this;
        /**
         * sample005: ViewMutableLiveData 使用例子
         * 4.修改 ViewMutableLiveData 变量的值。
         */
        BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // todo 根据广播更新蓝牙状态。
                if(BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(intent.getAction())) {
                    mBluetoothState.setValue(-1);
                }
                // ...
            }
        };
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
//        intentFilter.addAction();
        SystemUIApp.getMyApplicationContext().registerReceiver(broadcastReceiver, intentFilter);
    }

    public static synchronized StatusBarViewModel getInstance() {
        return sInstance;
    }

    public static synchronized StatusBarLiveData getViewLiveData() {
        if(null == mViewLiveData) {
            mViewLiveData = new StatusBarLiveData<Integer, String>();
        }
        return mViewLiveData;
    }

    /**
     * sample005: ViewMutableLiveData 使用例子
     * 2.定义访问 ViewMutableLiveData 变量的接口。
     * @return 反应 Bluetooth 状态的 ViewMutableLiveData 变量。
     */
    public static synchronized ViewMutableLiveData<Integer> getBluetoothViewLiveData() {
        if(null == mBluetoothState) {
            mBluetoothState = new ViewMutableLiveData<>();
        }
        return mBluetoothState;
    }
}
